/*
 * @Author: lixiang
 * @Date: 2020-05-24 01:01:18
 * @LastEditTime: 2020-09-27 14:32:44
 * @LastEditors: Please set LastEditors
 * @Description:
 * @FilePath: /wxapp_doctor/src/scripts/utils/uploadFile.js
 * @可以输入预定的版权声明、个性签名、空行等
 */
import { Toast } from "vant";
const Util = require("@/scripts/utils/util.js");
const env = require("./config.js"); // 配置文件，在这文件里配置你的OSS keyId和KeySecret,timeout:87600;

let OSS = require("ali-oss");

let client = new OSS({
  //云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用STS方式来进行API访问
  accessKeyId: "LTAI4GHCgX6WJ8x9k3qHEFsU",
  accessKeySecret: "trC0zcLt0tApfVCVrXcQa4A1tv3MdR",
  // stsToken: '<Your securityToken(STS)>',
  endpoint: "oss-cn-shanghai.aliyuncs.com/",
  bucket: "wsnjk",
  // cname: true,
  secure: true
});

export function uploadFile(data) {
  let file = data.file;

  data.status = "uploading";
  data.message = "上传中...";
  return new Promise((resolve, reject) => {
    let fileTypeIndex = file.name.lastIndexOf(".");
    let fileType = file.name.substring(fileTypeIndex);

    //文件存储路径 路径文件可自定义
    let storeAs = `2020health/${Util.formatTime(
      new Date()
    )}/${new Date().getTime()}${Math.floor(Math.random() * 150)}${fileType}`;

    client
      .multipartUpload(storeAs, file, {
        "Access-Control-Allow-Origin": "*"
      })
      .then(res => {
        resolve(res);
        data.status = "success";
        // file.message = '上传失败';
        // console.log(res)
      })
      .catch(err => {
        reject(err);
        console.log(err);
      });
  });
}
export default {
  uploadFile
};
